package com.ml.dao;

import com.ml.util.UtilProperties;

import java.sql.*;
import java.util.Properties;

/**
 * 读取考勤的Access数据的工具类
 * */
public class KqAccessBaseDao {
    public static String driver =  UtilProperties.getConfig("jdbc.driver.access.rmijdbc");
    public static String driver_odbc =  UtilProperties.getConfig("jdbc.driver.access");


    public static String ip_sh  =  UtilProperties.getConfig("jdbc.access.ip.sh");
    public static String dbname_sh =   UtilProperties.getConfig("jdbc.access.dbname.sh");
    public static String username_sh =  UtilProperties.getConfig("jdbc.access.username.sh");
    public static String password_sh =  UtilProperties.getConfig("jdbc.access.password.sh");


    public static String ip_dl  =  UtilProperties.getConfig("jdbc.access.ip.dl");
    public static String dbname_dl =   UtilProperties.getConfig("jdbc.access.dbname.dl");
    public static String username_dl =  UtilProperties.getConfig("jdbc.access.username.dl");
    public static String password_dl =  UtilProperties.getConfig("jdbc.access.password.dl");
    public static String PWD =  UtilProperties.getConfig("jdbc.access.password.dl.pwd");



    public static String ip_tj  =  UtilProperties.getConfig("jdbc.access.ip.tj");
    public static String dbname_tj =   UtilProperties.getConfig("jdbc.access.dbname.tj");
    public static String username_tj =  UtilProperties.getConfig("jdbc.access.username.tj");
    public static String password_tj =  UtilProperties.getConfig("jdbc.access.password.tj");
    public static String url_tj =  UtilProperties.getConfig("jdbc.url");



    /**
     * 上海 打开access数据库连接
     */
    public Connection openConnectSh(){
        Connection con = null;
        try {
            // 注册数据提供程序
            Class.forName(driver).newInstance();
            // 数据链接字符串
            String strurl = "jdbc:rmi://"+ip_sh+"/jdbc:odbc:"+dbname_sh;
            con = DriverManager.getConnection(strurl,username_sh,password_sh);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  con;
    }


    /**
     * 天津 打开access数据库连接
     */
    public Connection openConnectTj(){
        Connection con = null;
        try {
            Class.forName(driver_odbc);
            Properties pro = new Properties();//此方案解决中文乱码
            pro.setProperty("charSet","GB2312");
            pro.put( "charSet", "gbk" );
            //pro.put( "user", username );
            //pro.put( "password", password );
            con = DriverManager . getConnection( url_tj, pro );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  con;
    }


    /**
     * 大连 打开access数据库连接
     */
    public Connection openConnectDl(){
        Connection con = null;
        try {
            // 注册数据提供程序
            Class.forName(driver).newInstance();
            // 数据链接字符串
            String strurl  = "jdbc:rmi://"+ip_dl+"/jdbc:odbc:"+dbname_dl+";PWD="+PWD;
            con = DriverManager.getConnection(strurl,username_tj,password_tj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  con;
    }


    /**
     * 关闭access数据库连接
     */
    public  void closeConnect(Connection conn, Statement sta, PreparedStatement ps, ResultSet rst){
        try {
            if(rst!=null){
                rst.close();
            }
            if(sta!=null){
                sta.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(conn!=null){
                conn.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 关闭access数据库连接
     */
    public  void closeConnect(Connection conn){
        try {
            if(conn!=null){
                conn.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }




    /**
     * 只适合 增加 		删除	 	修改	操作
     * 执行SQL语句的方法
     */
    public void executeSh(String sql, Object param[]) {
        Connection conn=null;
        PreparedStatement ps=null;
        conn=this.openConnectSh();
        try {
            ps=conn.prepareStatement(sql);
            if(param!=null && param.length!=0){
                for(int i=0;i<param.length;i++){
                    //把每一个对象param放入数组中
                    ps.setObject((i+1), param[i]);
                }
            }
            ps.execute();//执行SQL语句
            this.closeConnect(conn, null, ps, null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 只适合 增加 		删除	 	修改	操作
     * 执行SQL语句的方法
     */
    public void executeTj(String sql, Object param[]) {
        Connection conn=null;
        PreparedStatement ps=null;
        conn=this.openConnectTj();
        try {
            ps=conn.prepareStatement(sql);
            if(param!=null && param.length!=0){
                for(int i=0;i<param.length;i++){
                    //把每一个对象param放入数组中
                    ps.setObject((i+1), param[i]);
                }
            }
            ps.execute();//执行SQL语句
            this.closeConnect(conn, null, ps, null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 只适合 增加 		删除	 	修改	操作
     * 执行SQL语句的方法
     */
    public void executeDl(String sql, Object param[]) {
        Connection conn=null;
        PreparedStatement ps=null;
        conn=this.openConnectDl();
        try {
            ps=conn.prepareStatement(sql);
            if(param!=null && param.length!=0){
                for(int i=0;i<param.length;i++){
                    //把每一个对象param放入数组中
                    ps.setObject((i+1), param[i]);
                }
            }
            ps.execute();//执行SQL语句
            this.closeConnect(conn, null, ps, null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
